package com.cheer.dao;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cheer.entity.OaArticleEntity;
import com.cheer.entity.OaLearnEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 文章
 *
 * @author cheer
 */
@Mapper
@Repository
public interface OaArticleDao extends BaseMapper<OaArticleEntity> {

    void incrSort(@Param("sort") Integer sort);

    void decrSort(@Param("sort") Integer sort);

    void incrSortRange(@Param("begin") Integer begin, @Param("end") Integer end);

    void decrSortRange(@Param("begin") Integer begin, @Param("end") Integer end);

    default IPage<OaArticleEntity> selectPageByArticleClass(Integer pageNum, Integer pageSize, Integer articleClass, String title, Integer id) {
        QueryWrapper<OaArticleEntity> queryWrapper = new QueryWrapper<>();

        if (articleClass != null) {
            queryWrapper.eq("article_class", articleClass);
        }

        if (ObjectUtil.isNotNull(id)) {
            queryWrapper.eq("id", id);
        }

        if (StrUtil.isNotBlank(title)) {
            queryWrapper.like("title", title);
        }

        queryWrapper.orderByAsc("sort").orderByDesc("add_time");

        return selectPage(new Page<>(pageNum, pageSize), queryWrapper);
    }

    default List<OaArticleEntity> selectListByRecommend(Integer pageNum, Integer pageSize) {
        QueryWrapper<OaArticleEntity> queryWrapper = new QueryWrapper<OaArticleEntity>()
                .eq("recommend", true)
                .eq("status", true)
                .orderByAsc("sort")
                .orderByDesc("add_time");

        int start = (pageNum - 1) * pageSize;
        int offset = pageSize;
        queryWrapper.last("limit " + start + ", " + offset);

        return selectList(queryWrapper);
    }

    default List<OaArticleEntity> selectListByArticleClassLimit(int start, int offset, Integer articleClass) {
        return selectList(new QueryWrapper<OaArticleEntity>()
                .eq("article_class", articleClass)
                .eq("status", true)
                .orderByAsc("sort")
                .orderByDesc("add_time")
                .last("limit " + start + "," + offset));
    }

    default String selectTitleById(Integer id) {
        OaArticleEntity articleEntity = selectOne(new QueryWrapper<OaArticleEntity>()
                .select("title")
                .eq("id", id)
                .last("limit 1"));
        if (articleEntity == null) {
            return "";
        } else {
            return articleEntity.getTitle();
        }
    }

    default String selectHrefById(Integer id) {
        OaArticleEntity articleEntity = selectOne(new QueryWrapper<OaArticleEntity>()
                .select("href")
                .eq("id", id)
                .last("limit 1"));
        if (articleEntity == null) {
            return null;
        }
        return articleEntity.getHref();
    }

    default List<OaArticleEntity> selectListByOrder() {
        return selectList(new QueryWrapper<OaArticleEntity>()
                .eq("status", true)
                .orderByAsc("sort")
                .orderByDesc("add_time")
                .last("limit 20"));
    }

    default List<OaArticleEntity> selectListByArticleClassAndOrder(Integer articleClass) {
        return selectList(new QueryWrapper<OaArticleEntity>()
                .eq("article_class", articleClass)
                .eq("status", true)
                .orderByAsc("sort")
                .orderByDesc("add_time")
                .last("limit 20"));
    }

    default OaArticleEntity getFirst(Integer articleClass) {
        return selectOne(new QueryWrapper<OaArticleEntity>()
                .eq("article_class", articleClass)
                .eq("status", true)
                .orderByAsc("sort")
                .last("limit 1"));
    }
}
